<template>
  <div id="app">
    <router-view v-if="isRouterAlive" />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  name: 'App',
  provide() {
    return {
      reload: this.reload,
      goUrl: this.goUrl,
      isEdit: this.isEdit,
      add: this.add,
      update: this.update,
      projectFormatter: this.projectFormatter,
      delStatus: this.delStatus,
      isNumeric: this.isNumeric,
      setStorage: this.setStorage,
      clearStorage: this.clearStorage,
      getStorage: this.getStorage,
      super: this.super,
      examine: this.examine,
      allow: this.allow,
      finish: this.finish
    }
  },
  data() {
    return {
      isRouterAlive: true
    }
  },
  computed: {
    ...mapGetters(['roles'])
  },
  methods: {
    reload() {
      this.isRouterAlive = false
      this.$nextTick(function() {
        this.isRouterAlive = true
      })
    },
    goUrl(url) {
      this.$router.push(url)
    },
    isEdit() {
      return Number(this.$route.params.id) || false
    },
    add(API, data, message, url) {
      API.create(data).then(res => {
        console.log(res)
        const resId = res.data.res
        if (res.data.error_code !== 1) {
          this.$message.error(res.data.message)
        } else {
          this.$message.success(message || '新增成功')
          if (url) {
            this.goUrl(url + resId)
          } else {
            this.$router.go(-1)
          }
        }
      })
    },
    update(API, data, message, url) {
      API.update(data).then(res => {
        if (res.data.error_code !== 1) {
          this.$message.error(res.data.message)
        } else {
          this.$message.success(message || '更新成功')
          if (url) {
            this.goUrl(url)
          } else {
            this.$router.go(-1)
          }
        }
      })
    },
    delStatus(row) {
      if (row.project.status === -99) {
        return `${row.project.name}[已删除]`
      } else {
        return `${row.project.name}`
      }
    },
    projectFormatter(row) {
      if (row.status === 1) {
        return this.$createElement('span', { style: { 'color': '#909399' }}, '未投标')
      } else if (row.status === 2) {
        return this.$createElement('span', { style: { 'color': '#279896' }}, '投标（未报价）')
      } else if (row.status === 3) {
        return this.$createElement('span', { style: { 'color': '#409EFF' }}, '投标（已报价）')
      } else if (row.status === 4) {
        return this.$createElement('span', { style: { 'color': '#e5ce10' }}, '中标')
      } else if (row.status === 5) {
        return this.$createElement('span', { style: { 'color': '#ff7e00' }}, '签合同')
      } else if (row.status === 100) {
        return this.$createElement('span', { style: { 'color': '#67C23A' }}, '已完成')
      } else if (row.status === -10) {
        return this.$createElement('span', { style: { 'color': '#F56C6C' }}, '未中标')
      }
    },
    isNumeric(value) {
      return !isNaN(parseFloat(value)) && isFinite(value)
    },
    setStorage(key, data) {
      window.localStorage.setItem(key, JSON.stringify(data))
    },
    clearStorage(key) {
      window.localStorage.removeItem(key)
    },
    getStorage(key) {
      const str = localStorage.getItem(key)
      const data = JSON.parse(str)
      return data
    },
    super() {
      return this.roles.includes(10000)
    },
    examine() {
      return this.roles.includes(100)
    },
    allow() {
      return this.roles.includes(1000)
    },
    finish() {
      return this.roles.includes(200)
    }
  }
}
</script>
